<template>
  <div>
    <div
      class="count up"            
      v-for="(card, index) in cards"
      :key="index"
      :style="card.style"
      :class="card.class"
    >
      <span class="current top" >{{ card.currentValue }}</span>
      <span class="next top"  v-show="card.nextValue !== -1">{{ card.nextValue }}</span>
      <span class="current bottom">{{ card.currentValue }}</span>
      <span class="next bottom" v-show="card.nextValue !== -1">{{ card.nextValue }}</span>
    </div>
  </div>
</template>

<script setup>
import _ from "underscore";
import { ref, onMounted } from "vue";

const cards = ref([
  { currentValue: 0, nextValue: -1, style: "left: 9%", class: []  },
  { currentValue: 0, nextValue: -1, style: "left: 15%", class: []  },
  { currentValue: 0, nextValue: -1, style: "left: 21%", class: []  },
  { currentValue: 0, nextValue: -1, style: "left: 27%", class: []  },
  { currentValue: 0, nextValue: -1, style: "left: 33%", class: [] },
  { currentValue: 0, nextValue: -1, style: "left: 39%", class: [] },
  { currentValue: 0, nextValue: -1, style: "left: 45%", class: [] },
]);

let index = cards.value.length - 1
let counter = 0;

onMounted(() => {
  // update(6, true);
});

function setupAnimation(card) {      
  const index = card.class.indexOf('changed')
  card.class.splice(index, 1)  
}

function executeAnimation(card) {  
  card.class.push('changing')
}

function finishAnimation(card) {
  card.class.push('changed')
  const index = card.class.indexOf('changing')
  card.class.splice(index, 1)  
}

function incrementCounter() {
  counter += 1;
  const counterStr = counter.toString().padStart(cards.value.length, "0");
  console.log(counterStr)
  // 遍历六张卡片，找出需要翻转的卡片  
  for (let i = 0; i < cards.value.length; i++) {  
   
    const nextValue = parseInt(counterStr[i]);

    if(cards.value[i].nextValue !== nextValue && cards.value[i].nextValue !== -1) {
      flipCard(cards.value[i])
    }

    cards.value[i].nextValue = nextValue
  
  
    if(nextValue === 1) {
      cards.value[i].currentValue = nextValue
    } else {
      cards.value[i].currentValue = nextValue === 0 ? nextValue : nextValue - 1    
    }

  
  }
}

function start(loop) {
  incrementCounter();
  flipCard(cards.value[index])
  if (loop) {
    _(start).delay(1000, true);
  }
}

function flipCard(card) {
  setupAnimation(card);
  _(executeAnimation).delay(20, card);
  _(finishAnimation).delay(900, card);
}


defineExpose({start})
</script>

<style>
.count {
  box-shadow: 0 10px 5px -5px rgba(0, 0, 0, 0.2);
  height: 300px;
  left: 45%;
  line-height: 300px;
  margin: -150px 0 -100px;
  -moz-perspective: 500px;
  -webkit-perspective: 500px;
  perspective: 500px;
  position: absolute;
  text-align: center;
  top: 50%;
  z-index: 1;
  transform: translateZ(0) scale(0.5);
  width: 200px;
}
.count span {
  background: #ffffff;
  color: #000000;
  display: block;
  font-size: 250px;
  left: 0;
  position: absolute;
  top: 0;
  text-shadow: 0 1px 0 #282828, 0 2px 0 #1e1e1e, 0 3px 0 #141414,
    0 4px 0 #0a0a0a, 0 5px 0 #000, 0 0 10px rgba(0, 0, 0, 0.8);
  -moz-transform-origin: 0 150px 0;
  -webkit-transform-origin: 0 150px 0;
  transform-origin: 0 150px 0;
  width: 100%;
}
.count span:before {
  /* border-bottom: 2px solid #000; */
  content: "";
  left: 0;
  position: absolute;
  width: 100%;
}
.count span:after {
  /* box-shadow: inset 0 0 60px rgba(0, 0, 0, 0.35); */
  content: "";
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  width: 100%;
}
.count .small {
  font-size: 175px;
}
.count .top {
  border-top-left-radius: 11px;
  border-top-right-radius: 11px;
  /* box-shadow: inset 0 2px rgba(0, 0, 0, 0.9), inset 0 3px 0 rgba(255, 255, 255, 0.4); */
  height: 50%;
  overflow: hidden;
}
.count .top:before {
  bottom: 0;
}
.count .top:after {
  background: -moz-linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));
  background: -webkit-linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));
  background: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.15));
  border-top-left-radius: 11px;
  border-top-right-radius: 11px;
}
.count .bottom {
  border-radius: 10px;
  height: 100%;
}
.count .bottom:before {
  top: 50%;
}
.count .bottom:after {
  border-radius: 10px;
  background: -moz-linear-gradient(
    rgba(255, 255, 255, 0.1),
    rgba(255, 255, 255, 0.1) 50%,
    rgba(255, 255, 255, 0)
  );
  background: -webkit-linear-gradient(
    rgba(255, 255, 255, 0.1),
    rgba(255, 255, 255, 0.1) 50%,
    rgba(255, 255, 255, 0)
  );
  background: linear-gradient(
    rgba(255, 255, 255, 0.1),
    rgba(255, 255, 255, 0.1) 50%,
    rgba(255, 255, 255, 0)
  );
}
.count.down .top {
  border-top-left-radius: 11px;
  border-top-right-radius: 11px;
  height: 50%;
}
.count.down .top.current {
  -moz-transform-style: flat;
  -webkit-transform-style: flat;
  transform-style: flat;
  z-index: 3;
}
.count.down .top.next {
  -moz-transform: rotate3d(1, 0, 0, -90deg);
  -ms-transform: rotate3d(1, 0, 0, -90deg);
  -webkit-transform: rotate3d(1, 0, 0, -90deg);
  transform: rotate3d(1, 0, 0, -90deg);
  z-index: 4;
}
.count.down .bottom {
  border-radius: 10px;
}
.count.down .bottom.current {
  z-index: 2;
}
.count.down .bottom.next {
  z-index: 1;
}
.count.down.changing .bottom.current {
  box-shadow: 0 75px 5px -20px rgba(0, 0, 0, 0.3);
  -moz-transform: rotate3d(1, 0, 0, 90deg);
  -ms-transform: rotate3d(1, 0, 0, 90deg);
  -webkit-transform: rotate3d(1, 0, 0, 90deg);
  transform: rotate3d(1, 0, 0, 90deg);
  -moz-transition: -moz-transform 0.35s ease-in, box-shadow 0.35s ease-in;
  -o-transition: -o-transform 0.35s ease-in, box-shadow 0.35s ease-in;
  -webkit-transition: -webkit-transform 0.35s ease-in, box-shadow 0.35s ease-in;
  transition: transform 0.35s ease-in, box-shadow 0.35s ease-in;
}
.count.down.changing .top.next,
.count.down.changed .top.next {
  -moz-transition: -moz-transform 0.35s ease-out 0.35s;
  -o-transition: -o-transform 0.35s ease-out 0.35s;
  -webkit-transition: -webkit-transform 0.35s ease-out;
  -webkit-transition-delay: 0.35s;
  transition: transform 0.35s ease-out 0.35s;
  -moz-transform: none;
  -ms-transform: none;
  -webkit-transform: none;
  transform: none;
}
.count.up .top {
  height: 50%;
}
.count.up .top.current {
  z-index: 4;
}
.count.up .top.next {
  z-index: 3;
}
.count.up .bottom.current {
  z-index: 1;
}
.count.up .bottom.next {
  box-shadow: 0 75px 5px -20px rgba(0, 0, 0, 0.3);
  -moz-transform: rotate3d(1, 0, 0, 90deg);
  -ms-transform: rotate3d(1, 0, 0, 90deg);
  -webkit-transform: rotate3d(1, 0, 0, 90deg);
  transform: rotate3d(1, 0, 0, 90deg);
  z-index: 2;
}
.count.up.changing .top.current {
  -moz-transform: rotate3d(1, 0, 0, -90deg);
  -ms-transform: rotate3d(1, 0, 0, -90deg);
  -webkit-transform: rotate3d(1, 0, 0, -90deg);
  transform: rotate3d(1, 0, 0, -90deg);
  -moz-transition: -moz-transform 0.2625s ease-in, box-shadow 0.2625s ease-in;
  -o-transition: -o-transform 0.2625s ease-in, box-shadow 0.2625s ease-in;
  -webkit-transition: -webkit-transform 0.2625s ease-in,
    box-shadow 0.2625s ease-in;
  transition: transform 0.2625s ease-in, box-shadow 0.2625s ease-in;
}
.count.up.changing .bottom.next,
.count.up.changed .bottom.next {
  box-shadow: 0 0 0 0 transparent;
  -moz-transition: box-shadow 0.175s cubic-bezier(0.375, 1.495, 0.61, 0.78)
      0.35s,
    -moz-transform 0.35s cubic-bezier(0.375, 1.495, 0.61, 0.78) 0.35s;
  -o-transition: box-shadow 0.175s cubic-bezier(0.375, 1.495, 0.61, 0.78) 0.35s,
    -o-transform 0.35s cubic-bezier(0.375, 1.495, 0.61, 0.78) 0.35s;
  -webkit-transition: box-shadow 0.175s cubic-bezier(0.375, 1.495, 0.61, 0.78),
    -webkit-transform 0.35s cubic-bezier(0.375, 1.495, 0.61, 0.78);
  -webkit-transition-delay: 0.35s, 0.35s;
  transition: box-shadow 0.175s cubic-bezier(0.375, 1.495, 0.61, 0.78) 0.35s,
    transform 0.35s cubic-bezier(0.375, 1.495, 0.61, 0.78) 0.35s;
  -moz-transform: rotate3d(1, 0, 0, 0);
  -ms-transform: rotate3d(1, 0, 0, 0);
  -webkit-transform: rotate3d(1, 0, 0, 0);
  transform: rotate3d(1, 0, 0, 0);
}
.count.changed .top.current,
.count.changed .bottom.current {
  display: none;
}
</style>
